package com.example.theadadmin.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;


import javax.crypto.SecretKey;

public class JWTTokenUtil {

    public static SecretKey key = Jwts.SIG.HS256.key().build();

    // 创建令牌token
    public static String getToken(String username) {
        String jws = Jwts.builder().subject(username).signWith(key).compact();
        return jws;
    }

    // 解析令牌token
    public static String parseToken(String token) throws Exception {

            Jws<Claims> jws = Jwts.parser().verifyWith(key).build().parseSignedClaims(token);
            return jws.getPayload().getSubject();
    }

    public static void main(String[] args) throws Exception {
        String token = getToken("admin");

        System.out.println(token);

        System.out.println(parseToken(token));

    }
}
